﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using justice.Models;
using justice.ViewModel;

namespace justice.Controllers
{
    [Authorize]
    public class ForensicOpinionController : Controller
    {
       justiceEntities dal=new justiceEntities();
        public ActionResult ForensicOpinionIndex(int? id)
        {
            List<ForensicOpinion> foslist = dal.ForensicOpinion.ToList();
            List<Shroff_book> sblist = dal.Shroff_book.ToList();
            List<be_appraiser> balist = dal.be_appraiser.ToList();
            List<justice_user> julist = dal.justice_user.ToList();
            List<judiciary> jclist = dal.judiciary.ToList();
            var data = from fos in foslist
                       join sb in sblist on fos.MainInfo equals sb.shroff_id
                       join ba in balist on sb.main_case equals ba.user_id
                       join ju in julist on sb.enter_user_id equals ju.id
                       join jc in jclist on ju.unit_id equals jc.id
                       where fos.MainInfo==id
                       select new forensicopinion_shroffbook
                       {
                           shroffbook_id=sb.shroff_id,
                           shroffbook_number = sb.shroff_number,
                           client = sb.client,
                           consignation_transaction = sb.consignation_transaction,
                           shroff_material = sb.shroff_material,
                           acceptdate = sb.consignation_date,
                           shroff_data = fos.Shroff_data,
                           identifylocation = jc.judiciaryName,
                           be_appraiser = ba.username,
                           testcase = fos.Testcase,
                           inspectionprocess=fos.Inspectionprocess,
                           analysishows=fos.Analysishows,
                           expertopinion=fos.Expertopinion,
                           judger=fos.Judger
                       };
            int uid = (int)Session["uid"];
            justice_user user = dal.justice_user.ToList().Find(l => l.id == uid);
            Role r = dal.Role.ToList().Find(l => l.id == user.role_id);
            ViewBag.role = r.RoleName;
            return View("ForensicOpinionIndex", data.ToList()[0]);
         }  
        public ActionResult AddForensicOpinionInfo(int id)
        {
            var data = from fos in dal.ForensicOpinion
                       join sb in dal.Shroff_book on fos.MainInfo equals sb.shroff_id
                       join ba in dal.be_appraiser on sb.main_case equals ba.user_id
                       join ju in dal.justice_user on sb.enter_user_id equals ju.id
                       join jc in dal.judiciary on ju.unit_id equals jc.id
                       where fos.MainInfo == id
                       select new forensicopinion_shroffbook
                       {
                           shroffbook_id = sb.shroff_id,
                           shroffbook_number = sb.shroff_number,
                           client = sb.client,
                           consignation_transaction = sb.consignation_transaction,
                           shroff_material = sb.shroff_material,
                           acceptdate = sb.consignation_date,
                           shroff_data = fos.Shroff_data,
                           identifylocation = jc.judiciaryName,
                           be_appraiser = ba.username,
                           testcase = fos.Testcase,
                           inspectionprocess = fos.Inspectionprocess,
                           analysishows = fos.Analysishows,
                           expertopinion = fos.Expertopinion,
                           judger = fos.Judger
                        };
            forensicopinion_shroffbook forr = data.ToList()[0];
            if (forr.testcase != null)
            {
                forr.testcase = forr.testcase.Replace("<br />","\r\n");
            }
            if (forr.inspectionprocess != null)
            {
                forr.inspectionprocess = forr.inspectionprocess.Replace("<br />","\r\n");
            }
            if (forr.analysishows != null)
            {
                forr.analysishows = forr.analysishows.Replace("<br />", "\r\n");
            }
            return View("AddForensicOpinionInfo", forr);
        }
        public ActionResult AddForensicOpinion(int MainInfo_id,string shroffdata, string testcase,string inspectionprocess,string analysishows,string expertopinion,string judger)
        {
            
            ForensicOpinion fo = dal.ForensicOpinion.ToList().Find(l => l.MainInfo == MainInfo_id);
            fo.MainInfo = MainInfo_id;
            fo.Testcase = testcase.Replace("\r\n", "<br />");
            fo.Inspectionprocess = inspectionprocess.Replace("\r\n", "<br />");
            fo.Analysishows = analysishows.Replace("\r\n", "<br />");
            fo.Expertopinion = expertopinion;
            fo.Judger = judger;
            fo.Shroff_data = shroffdata;
            dal.SaveChanges();
            return Redirect("/ForensicOpinion/AddForensicOpinionInfo/" + MainInfo_id);
        }
    }
}